class Solution {
public:
    int intersectionSizeTwo(vector<vector<int>>& intervals) {
        sort(intervals.begin(), intervals.end(), [](vector<int>& a, vector<int>& b) {
            return a[1] < b[1];
            });

        int res = 0;
        int first = -2, second = -1;

        for (auto& in : intervals) {
            if (in[0] > second) {
                res += 2;
                second = in[1], first = in[1] - 1;
            }
            else if (in[1] > second && in[0] > first) {
                first = second;
                res++;
                second = in[1];
            }
            else if (in[0] > first) {
                first = second - 1;
                res++;
            }
            //cout << first << ' ' << second << endl;
        }
        return res;
    }
};